﻿using System;
using System.Data;
using System.Windows.Forms;
using org.se.fs.presenter.IView.Profile;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.View.Profile;

namespace org.se.fs.view.Profile
{
    public partial class FrmClientInstitutionSubMenu : Form, IClientInstitutionSubMenu
    {
        #region Public Property Variable
        public void SetLabel(string status)
        {
            label.Text = status;
        }
        public void ButtonAddVisible(bool enable)
        {
            btnAdd.Visible = enable;
        }
        public void ButtonUpdateVisible(bool enable)
        {
            btnUpdate.Visible = enable;
        }
        public void ButtonAddEnable(bool enable)
        {
            btnAdd.Enabled = enable;
        }
        public void ButtonUpdateEnable(bool enable)
        {
            btnUpdate.Enabled = enable;
        }
        public int IndexInstitution
        {
            get { return cmbInstitution.SelectedIndex; }
            set { cmbInstitution.SelectedIndex = value; }
        }
        public string Department
        {
            get { return txtDepartment.Text; }
            set { txtDepartment.Text = value; }
        }
        public bool Employed
        {
            get { return chkEmplyed.Checked; }
            set { chkEmplyed.Checked = value; }
        }
        public decimal Salary
        {
            get { return nudSalary.Value; }
            set { nudSalary.Value = value; }
        }
        public string Position
        {
            get { return txtPosition.Text; }
            set { txtPosition.Text = value; }
        }
        public void FillInstitution(object[] data)
        {
            cmbInstitution.Items.Clear();
            cmbInstitution.Items.AddRange(data);
        }
        public void FocusInstitution()
        {
            cmbInstitution.Focus();
        }
        public void FocusDepartment()
        {
            txtDepartment.Focus();
        }
        public void FocusEmployed()
        {
            chkEmplyed.Focus();
        }
        public void FocusSalary()
        {
            nudSalary.Focus();
        }
        public void FocusPosition()
        {
            txtPosition.Focus();
        }
        public bool IsModified { get; set; }
        #endregion

        private readonly ClientInstitutionSubMenu _presenter;
        private DataTable _returnValue;

        public FrmClientInstitutionSubMenu()
        {
            InitializeComponent();
            _presenter = new ClientInstitutionSubMenu(this);
            _presenter.OnAdd += PresenterOnAdd;
            _presenter.OnError += PresenterOnError;
            _presenter.OnModify += PresenterOnModify;
            _presenter.OnValidate += PresenterOnValidate;
        }

        static void PresenterOnValidate(object source, FormEvent e)
        {
            if (!e.IsSuccess)
                MessageBox.Show(e.Exception.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static DataTable FormShow(UInt64 clientId)
        {
            using (var frmForm = new FrmClientInstitutionSubMenu())
            {
                frmForm._presenter.SetClientId(clientId);
                frmForm.ShowDialog();
                return frmForm._returnValue;
            }
        }

        public static DataTable FormShow(DataTable data)
        {
            using (var frmForm = new FrmClientInstitutionSubMenu())
            {
                frmForm._presenter.Data = data;
                frmForm.ShowDialog();
                return frmForm._returnValue;
            }
        }

        private void BtnSubmitClick(object sender, EventArgs e)
        {
            _presenter.Add();
        }

        private void BtnCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnUpdateClick(object sender, EventArgs e)
        {
            _presenter.Modify();
        }

        private void FrmFamilyBackgroundSubMenuLoad(object sender, EventArgs e)
        {
            _presenter.Initialize();
        }

        public void PresenterOnError(object source, FormEvent e)
        {
            if (!e.IsSuccess)
                MessageBox.Show(e.Exception.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void PresenterOnAdd(object source, FormEvent e)
        {
            MessageBox.Show(e.Exception.Message, e.IsSuccess ? @"Success" : @"Error", MessageBoxButtons.OK,
                            e.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            
            if (!e.IsSuccess) return;
            _returnValue = (DataTable) e.Value;
            Close();
        }

        public void PresenterOnModify(object source, FormEvent e)
        {
            MessageBox.Show(e.Exception.Message, e.IsSuccess ? @"Success" : @"Error", MessageBoxButtons.OK,
                            e.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error);

            if (!e.IsSuccess) return;
            _returnValue = (DataTable)e.Value;
            Close();
        }

        private void ChkEmplyedCheckedChanged(object sender, EventArgs e)
        {
            chkEmplyed.Text = chkEmplyed.Checked ? "YES" : "NO";
        }
    }
}